angular.module('wf-utils')
.service('menuManage', ($rootScope,$localStorage)->
    states = {
        #欢迎页/控制台
        'home':{
            name:'主页',
            icon:'glyphicon-home',
            url:'home/home.html',
            controller:'HomeCtrl'
        }
        'about-nr':{
            name:'关于凝睿',
            icon:'glyphicon-home',
            url:'about-nr/about-nr.html',
            controller:'AboutNrCtrl'
        }
    }
    menuList = [
        {
            name: "关于凝睿"
            enName: "About NR"
            type: "page"
            route: "about-nr"
            childs: [
                {
                    name: "凝睿价值"
                    enName: "Value"
                    type: "subPage"
                    route: "about-nr"
                    id: 0
                }
                {
                    name: "帮助中心"
                    enName: "Help"
                    type: "subPage"
                    route: "about-nr"
                    id: 1
                }
                {
                    name: "法律声明"
                    enName: "Statement"
                    type: "subPage"
                    route: "about-nr"
                    id: 2
                }
                {
                    name: "工作机会"
                    enName: "Job in NR"
                    type: "subPage"
                    route: "about-nr"
                    id: 3
                }
                {
                    name: "合作伙伴"
                    enName: "Partner"
                    type: "subPage"
                    route: "about-nr"
                    id: 4
                }
            ]
        }
        {
            name: "产品服务"
            enName: "Service"
            type: "page"
            route: "product-service"
            childs: [
                {
                    name: $localStorage['wainCN'][0].title
                    enName: $localStorage['wainEN'][0].title
                    type: "subPage"
                    route: "product-service"
                    id: if $localStorage['language'] == 0 then $localStorage['wainCN'][0].id else $localStorage['wainEN'][0].id
                }
                {
                    name: $localStorage['wainCN'][1].title
                    enName: $localStorage['wainEN'][1].title
                    type: "subPage"
                    route: "product-service"
                    id: if $localStorage['language'] == 0 then $localStorage['wainCN'][1].id else $localStorage['wainEN'][1].id
                }
                {
                    name: $localStorage['wainCN'][2].title
                    enName: $localStorage['wainEN'][2].title
                    type: "subPage"
                    route: "product-service"
                    id: if $localStorage['language'] == 0 then $localStorage['wainCN'][2].id else $localStorage['wainEN'][2].id
                }
                {
                    name: $localStorage['wainCN'][3].title
                    enName: $localStorage['wainEN'][3].title
                    type: "subPage"
                    route: "product-service"
                    id: if $localStorage['language'] == 0 then $localStorage['wainCN'][3].id else $localStorage['wainEN'][3].id
                }
                {
                    name: $localStorage['wainCN'][4].title
                    enName: $localStorage['wainEN'][4].title
                    type: "subPage"
                    route: "product-service"
                    id: if $localStorage['language'] == 0 then $localStorage['wainCN'][4].id else $localStorage['wainEN'][4].id
                }

                {
                    name: $localStorage['wainCN'][5].title
                    enName: $localStorage['wainEN'][5].title
                    type: "subPage"
                    route: "product-service"
                    id: if $localStorage['language'] == 0 then $localStorage['wainCN'][5].id else $localStorage['wainEN'][5].id
                }
                {
                    name: $localStorage['wainCN'][6].title
                    enName: $localStorage['wainEN'][6].title
                    type: "subPage"
                    route: "product-service"
                    id: if $localStorage['language'] == 0 then $localStorage['wainCN'][6].id else $localStorage['wainEN'][6].id
                }
            ]
        }
        {
            name: "服务支持"
            enName: "Support"
            type: ""
            route: ""
            childs: [
                {
                    name: "产品资料"
                    enName: "Product Manual"
                    type: "page"
                    route: "product-info"
                }
                {
                    name: "技术文档"
                    enName: "Technical Docs"
                    type: "page"
                    route: "tec-docs"
                }
            ]
        }
        {
            name: "样品中心"
            enName: "Sample Center"
            type: "page"
            route: "demo-center"
            childs: [
            ]
        }
        {
            name: "商城"
            enName: "Mall"
            type: ""
            route: ""
            childs: [
                {
                    name: "凝睿商城"
                    enName: "Official Mall"
                    type: "link"
                    route: "http://www.nr-esc.com/store.php"
                }
                {
                    name: "凝睿阿里巴巴"
                    enName: "Ali Mall"
                    type: "link"
                    route: "https://nresc.1688.com"
                }
                {
                    name: "凝睿淘宝"
                    enName: "Taobao Mall"
                    type: "link"
                    route: "https://nr-esc.taobao.com"
                }

            ]
        }
    ]
    footerList = [
        {
            name: "关于凝睿"
            enName: "About NR"
            type: "page"
            route: "about-nr"
            childs: [
                {
                    name: "凝睿价值"
                    enName: "Value"
                    type: "subPage"
                    route: "about-nr"
                    id: 0
                }
                {
                    name: "帮助中心"
                    enName: "Help"
                    type: "subPage"
                    route: "about-nr"
                    id: 1
                }
                {
                    name: "法律声明"
                    enName: "Statement"
                    type: "subPage"
                    route: "about-nr"
                    id: 2
                }
                {
                    name: "工作机会"
                    enName: "Job in NR"
                    type: "subPage"
                    route: "about-nr"
                    id: 3
                }
                {
                    name: "合作伙伴"
                    enName: "Partner"
                    type: "subPage"
                    route: "about-nr"
                    id: 4
                }
            ]
        }
        {
            name: "新闻活动"
            enName: "News&Activity"
            type: "page"
            route: "news-activity"
            childs: [
                {
                    name: "公司新闻"
                    enName: "Company News"
                    type: "subPage"
                    route: "news-activity"
                    id: 0
                }
                {
                    name: "新品到货"
                    enName: "New Product"
                    type: "subPage"
                    route: "news-activity"
                    id: 1
                }
                {
                    name: "行业新闻"
                    enName: "Industry News"
                    type: "subPage"
                    route: "news-activity"
                    id: 2
                }
                {
                    name: "活动信息"
                    enName: "Activities"
                    type: "subPage"
                    route: "news-activity"
                    id: 3
                }
            ]
        }
        {
            name: "技术文档"
            enName: "Technical Docs"
            type: "page"
            route: "tec-docs"
            childs: [
                {
                    name: "元器件选择"
                    enName: "Component Selection"
                    type: "subPage"
                    route: "tec-docs"
                    id:1
                }
                {
                    name: "应用笔记"
                    enName: "Application Notes"
                    type: "subPage"
                    route: "tec-docs"
                    id:2
                }
                {
                    name: "设计指南"
                    enName: "Design Notes"
                    type: "subPage"
                    route: "tec-docs"
                    id:3
                }
                {
                    name: "解决方案"
                    enName: "Solutions"
                    type: "subPage"
                    route: "tec-docs"
                    id:4
                }
                {
                    name: "焊接知识"
                    enName: "PCBA Items"
                    type: "subPage"
                    route: "tec-docs"
                    id:5
                }
                {
                    name: "其他"
                    enName: "Others"
                    type: "subPage"
                    route: "tec-docs"
                    id:6
                }
            ]
        }
        {
            name: "产品资料"
            enName: "Product Manual"
            type: "page"
            route: "product-info"
            childs: [
                {
                    name: "无源元件"
                    enName: "Passive Component"
                    type: "subPage"
                    route: "product-info"
                    id: 1
                }
                {
                    name: "有源元件"
                    enName: "Positive Component"
                    type: "subPage"
                    route: "product-info"
                    id: 2
                }
                {
                    name: "元件盒/样品盒"
                    enName: "Sample Box"
                    type: "subPage"
                    route: "product-info"
                    id: 3
                }
                {
                    name: "工具"
                    enName: "Tools"
                    type: "subPage"
                    route: "product-info"
                    id: 4
                }
                {
                    name: "软件"
                    enName: "Software"
                    type: "subPage"
                    route: "product-info"
                    id: 5
                }
                {
                    name: "行业规范"
                    enName: "Industry Standard"
                    type: "subPage"
                    route: "product-info"
                    id: 6
                }
                {
                    name: "其他"
                    enName: "Others"
                    type: "subPage"
                    route: "product-info"
                    id: 7
                }
            ]
        }
        {
            name: "样品中心"
            enName: "Sample Center"
            type: "page"
            route: "demo-center"
            id: 9527
            childs: [
                {
                    name: "原厂品质"
                    enName: "Original"
                    type: "subPage"
                    route: "demo-center"
                    id: 0
                }
                {
                    name: "现货库存"
                    enName: "Stock"
                    type: "subPage"
                    route: "demo-center"
                    id: 1
                }
                {
                    name: "快速响应"
                    enName: "Speed"
                    type: "subPage"
                    route: "demo-center"
                    id: 2
                }
                {
                    name: "一片起订"
                    enName: "One Piece"
                    type: "subPage"
                    route: "demo-center"
                    id: 3
                }
                {
                    name: "元件盒"
                    enName: "Sample Box"
                    type: "subPage"
                    route: "demo-center"
                    id: 4
                }
            ]
        }
    ]
    get: ()->
        menuList
    getFooterList: ()->
        footerList

    getRoleMenu: (roleID)->
        menuList
    goState:(state,reload)->
        if states[state]
            item={
                href:state
                name:states[state].name
                icon:states[state].icon
            }
            this.goPage(item,reload)
    goPage: (item,reload)->
        href = item.href
        tmp = _.find $rootScope.navTabs.arrs, (tab)-> tab.href == href
        if !tmp
            $rootScope.navTabs.arrs.push {href: href, name: item.name, icon: item.icon}
        else
            if reload
                $rootScope.navTabs.arrs=_.filter($rootScope.navTabs.arrs,(tab)->tab.href!=href)
                $rootScope.navTabs.history = _.without $rootScope.navTabs.history, href
                $rootScope.navTabs.arrs.push {href: href, name: item.name, icon: item.icon}
        $rootScope.navTabs.active = href
        tmpHistory = _.find $rootScope.navTabs.history, (history)-> history == href
        if tmpHistory
            $rootScope.navTabs.history = _.without $rootScope.navTabs.history, tmpHistory
        $rootScope.navTabs.history.push href
#        console.log('navTabs:', $rootScope.navTabs)
    genViews: ()->
        views = {"":{templateUrl:'templates/main/main.html',controller:'EdMainCtrl'}}
        _.each states, (item,param)->
#            console.log('state:',param)
            views[param + '@main'] = {
                templateUrl: 'templates/' + item.url
                controller: item.controller
            }
#        console.log(views)
        views
    closeTab:(event,href)->
        if event
            event.preventDefault()
        $rootScope.navTabs.history=_.filter $rootScope.navTabs.history,(history)->history!=href
        $rootScope.navTabs.arrs=_.filter $rootScope.navTabs.arrs,(tab)->tab.href!=href
        if $rootScope.navTabs.active==href and $rootScope.navTabs.history.length>=1
            $rootScope.navTabs.active=$rootScope.navTabs.history[$rootScope.navTabs.history.length-1]
)